package com.todod.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ProjectFileEvaluateDto;
import com.todod.entity.QueryProjectFile;
import com.todod.mapper.ProjectFileEvaluateMapper;
import com.todod.model.ProjectFileEvaluate;
import com.todod.model.User;

@Service
@Transactional
public class ProjectFileEvaluateService {

	@Autowired
	private ProjectFileEvaluateMapper projectFileEvaluateMapper;
	@Autowired
	private IUserService iUserService;

	/**
	 * 
	 * @param _userId
	 * @param queryProjectFileEvaluate
	 * @return
	 */
	public IPage<ProjectFileEvaluateDto> getListPage(Long _userId, QueryProjectFile queryProjectFileEvaluate) {

		Page<ProjectFileEvaluateDto> page = new Page<>(queryProjectFileEvaluate.getPage(),
				queryProjectFileEvaluate.getLimit());

		IPage<ProjectFileEvaluateDto> iPage = projectFileEvaluateMapper.getListPage(page, _userId,
				queryProjectFileEvaluate);

		return iPage;
	}

	/**
	 * 
	 * @param _userId
	 * @param projectFileEvaluate
	 * @param value
	 * @param value2
	 */

	public void saveData(Long _userId, ProjectFileEvaluate projectFileEvaluate) {

		projectFileEvaluate.setId(SnowflakeIdGenerator.getId());
		projectFileEvaluate.setEvaluatorId(_userId);

		projectFileEvaluate.setCreatetimeTi(LocalDateTime.now());
		projectFileEvaluate.setCreateuserId(_userId);
		projectFileEvaluateMapper.insert(projectFileEvaluate);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public Map<String, Object> getInfo(Long code) {
		Map<String, Object> map = new HashMap<>();
		ProjectFileEvaluateDto projectFileEvaluate = projectFileEvaluateMapper.getInfo(code);
		map.put("projectFileEvaluate", projectFileEvaluate);
		return map;
	}

	/**
	 * 
	 * @param queryProjectFileEvaluate
	 * @return
	 */
	public List<ProjectFileEvaluate> getEvaluateList(Long evaluatorId, Long fileId) {
		QueryWrapper<ProjectFileEvaluate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("xfe_evaluator_mid", evaluatorId);
		queryWrapper.eq("xfe_file_mid", fileId);
		queryWrapper.eq("xfe_state", "1");

		List<ProjectFileEvaluate> list = projectFileEvaluateMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public Object getEvaluateList(Long fileId) {
		Map<String, Object> map = new HashMap<>();
		List<ProjectFileEvaluateDto> list = projectFileEvaluateMapper.getEvaluateList(fileId);

		BigDecimal score = new BigDecimal(0);

		if (list != null && list.size() > 0) {
			for (ProjectFileEvaluateDto evaluate : list) {
				User user = iUserService.getById(evaluate.getEvaluatorId());
				evaluate.setEvaluatorName(user.getNickName());

				score = score.add(new BigDecimal(evaluate.getEvaluationScore()));
			}

			score = score.divide(new BigDecimal(list.size()));
		}

		map.put("evaluationScore", score);
		map.put("projectFileEvaluate", list);
		return map;
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public List<ProjectFileEvaluate> getEvaluateListByFileId(Long fileId) {
		QueryWrapper<ProjectFileEvaluate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("xfe_file_mid", fileId);

		List<ProjectFileEvaluate> list = projectFileEvaluateMapper.selectList(queryWrapper);

		return list;
	}

}
